package cc.rengu.igas.bsps.core.service.trans;


import cc.rengu.igas.bsps.common.dao.ManagementStatisAnalyseMapper;
import cc.rengu.igas.bsps.common.dao.impl.ManagementStatisAnalyseMapperImpl;
import cc.rengu.igas.bsps.common.entity.PayTypeStatisInfo;
import cc.rengu.igas.bsps.common.entity.TransSumInfoAnalyse;
import cc.rengu.igas.bsps.common.enums.BspsMchntTypeEnum;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.PayTypeStatisInfoBean;
import cc.rengu.igas.bsps.facade.bean.TransSumInfoAnalyseBean;
import cc.rengu.igas.bsps.facade.request.ManagementStatisAnalyseRequest;
import cc.rengu.igas.bsps.facade.response.ManagementStatisAnalyseResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 经营统计分析
 * Created by yuhongwen
 */
public class ManagementStatisAnalyseService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        ManagementStatisAnalyseRequest managementStatisAnalyseRequest = new ManagementStatisAnalyseRequest();
        ConvertUtil.convertOutput(managementStatisAnalyseRequest);
        return managementStatisAnalyseRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ManagementStatisAnalyseRequest managementStatisAnalyseRequest = (ManagementStatisAnalyseRequest) request;
        ManagementStatisAnalyseResponse managementStatisAnalyseResponse = new ManagementStatisAnalyseResponse();

        String instId = managementStatisAnalyseRequest.getHeader().getInstId();
        String mchntNo = managementStatisAnalyseRequest.getMchntNo();
        String startDate = managementStatisAnalyseRequest.getStartDate();
        String endDate = managementStatisAnalyseRequest.getEndDate();
        String queryType = managementStatisAnalyseRequest.getQueryType();
        String userId = managementStatisAnalyseRequest.getUserId();
        String mchntType = managementStatisAnalyseRequest.getMchntType();
        String mchntStoreNo = managementStatisAnalyseRequest.getMchntStoreNo();

        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(startDate)
                || StringUtil.isEmptyOrNull(endDate) || StringUtil.isEmptyOrNull(queryType) || StringUtil.isEmptyOrNull(userId)) {
            rglog.error("必填性信息校验失败");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        List<TransSumInfoAnalyseBean> transSunInfoList = new ArrayList<>();
        List<PayTypeStatisInfoBean> payTypeStatisInfoList = new ArrayList<>();
        List<TransSumInfoAnalyse> transSunInfos;
        List<PayTypeStatisInfo> payTypeStatisInfos;

        ManagementStatisAnalyseMapper managementStatisAnalyseMapper = new ManagementStatisAnalyseMapperImpl();
        String statisType = "0";
        if ("02".equals(queryType)) {
            if (startDate.length() > 6 || endDate.length() > 6) {
                rglog.error("日期参数格式错误");
            }
            statisType = "1";
            startDate = DateUtil.dateStringFormatConvert(startDate, "yyyyMMdd", "yyyyMM");
            endDate = DateUtil.dateStringFormatConvert(endDate, "yyyyMMdd", "yyyyMM");
        }
        rglog.info("打印门店号:<{}>", mchntStoreNo);
        if (!StringUtil.isEmptyOrNull(mchntStoreNo)) {
            rglog.info("查询门店商户信息");
            mchntNo = mchntStoreNo;
        }
        /*交易信息查询*/
        transSunInfos = managementStatisAnalyseMapper.selectTransSunInfoList(instId, mchntNo, startDate, endDate, statisType, mchntType);
        if (!CollectionUtils.isEmpty(transSunInfos)) {
            rglog.info("商户交易统计信息赋值");
            for (TransSumInfoAnalyse transSumInfoAnalyse : transSunInfos) {
                TransSumInfoAnalyseBean transSumInfoAnalyseBean = new TransSumInfoAnalyseBean();
                transSumInfoAnalyseBean.setTxnDate(transSumInfoAnalyse.getTxnDate());
                if (!StringUtil.isEmptyOrNull(transSumInfoAnalyse.getPayAmt())) {
                    transSumInfoAnalyseBean.setPayAmt(new BigDecimal(transSumInfoAnalyse.getPayAmt()).toString());
                }
                if (!StringUtil.isEmptyOrNull(transSumInfoAnalyse.getAvgTransAmt())) {
                    transSumInfoAnalyseBean.setAvgTransAmt(new BigDecimal(transSumInfoAnalyse.getAvgTransAmt()).toString());
                }
                transSumInfoAnalyseBean.setPayNum(transSumInfoAnalyse.getPayNum());

                if (!StringUtil.isEmptyOrNull(transSumInfoAnalyse.getRemark1())) {
                    transSumInfoAnalyseBean.setRefundPayAmt(new BigDecimal(transSumInfoAnalyse.getRemark1()).toString());
                }
                if (!StringUtil.isEmptyOrNull(transSumInfoAnalyse.getRemark3())) {
                    transSumInfoAnalyseBean.setRefundAvgTransAmt(new BigDecimal(transSumInfoAnalyse.getRemark3()).toString());
                }
                transSumInfoAnalyseBean.setRefundPayNum(transSumInfoAnalyse.getRemark2());

                transSunInfoList.add(transSumInfoAnalyseBean);
            }
        }
        if (!BspsMchntTypeEnum.AGENT_MCHNT.getMchntType().equals(mchntType) && !BspsMchntTypeEnum.PLAT_MCHNT.getMchntType().equals(mchntType) && !BspsMchntTypeEnum.GROUP_MCHNT.getMchntType().equals(mchntType)) {
            /*支付方式信息查询,仅普通商户查询*/
            String payTypeSum = managementStatisAnalyseMapper.selectPayTypeStatis(instId, mchntNo, startDate, endDate, statisType);
            BigDecimal payPer = BigDecimal.valueOf(0.0000);
            BigDecimal sumPer = new BigDecimal(1);
            String payTypePer;
            payTypeStatisInfos = managementStatisAnalyseMapper.selectPayTypeStatisInfoList(instId, mchntNo, startDate, endDate, statisType);
            if (!CollectionUtils.isEmpty(payTypeStatisInfos) && !StringUtil.isEmptyOrNull(payTypeSum)) {
                for (int m = 0; m < payTypeStatisInfos.size(); m++) {
                    PayTypeStatisInfoBean payTypeStatisInfoBean = new PayTypeStatisInfoBean();
                    payTypeStatisInfoBean.setPayType(payTypeStatisInfos.get(m).getPayType());
                    payTypeStatisInfoBean.setPayTypeAmt(new BigDecimal(payTypeStatisInfos.get(m).getPayTypeAmt()).toString());
                    payTypeStatisInfoBean.setPayTypeNum(payTypeStatisInfos.get(m).getPayTypeNum());
                    if (m != payTypeStatisInfos.size() - 1) {
                        payTypePer = AmountUtil.division(payTypeStatisInfos.get(m).getPayTypeAmt(), payTypeSum, 4);
                        payPer = payPer.add(new BigDecimal(payTypePer));
                    } else {
                        payTypePer = sumPer.subtract(payPer).toString();
                    }
                    payTypeStatisInfoBean.setPayTypeRatio(AmountUtil.multiplication(payTypePer, "100", 2));
                    payTypeStatisInfoList.add(payTypeStatisInfoBean);
                }
            }
        }

        if (!CollectionUtils.isEmpty(transSunInfoList)) {
            managementStatisAnalyseResponse.setTransSunInfoList(transSunInfoList);
        } else {
            rglog.error("未查询到交易统计信息!");
        }
        if (!CollectionUtils.isEmpty(payTypeStatisInfoList)) {
            managementStatisAnalyseResponse.setPayTypeStatisInfoList(payTypeStatisInfoList);
        } else {
            rglog.error("未查询到支付方式统计信息!");
        }
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<ManagementStatisAnalyseResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(managementStatisAnalyseResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ManagementStatisAnalyseResponse tradeDetailListQueryResponse = (ManagementStatisAnalyseResponse) bizResponse.getResult();
        ConvertUtil.convertInput(tradeDetailListQueryResponse);
    }
}

